﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Salon
{
    class UserService
    {
        public UserService()
        { 
            
        }

        public User saveUser(User user)
        {
            String insertQuery = "INSERT INTO users VALUES(@name,@surname,@country,@birth_date," +
            "@role,@active,@salon,@login,@pass,@city,@street,@zip,@flat_number)";

            String updateQuery = "UPDATE users SET name=@name,surname=@surname,country=@country,birth_date=@birth_date," +
            "role=@role,active=@active,salon=@salon,login=@login,pass=@pass,city=@city,street=@street,zip=@zip,flat_number=@flat_number " +
            " WHERE id=@id";

            SqlCommand cmd = SqlService.getInstance().getCommand(( user.Id <1) ? insertQuery : updateQuery);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@name", user.Name);
            cmd.Parameters.AddWithValue("@surname", user.SurName);
            cmd.Parameters.AddWithValue("@country", user.Country);
            cmd.Parameters.AddWithValue("@birth_date", user.BirthDate.Date);
            cmd.Parameters.AddWithValue("@role", user.Role);
            cmd.Parameters.AddWithValue("@active", 1);
            cmd.Parameters.AddWithValue("@salon", user.Salon.Id);   
            cmd.Parameters.AddWithValue("@login", user.Login);
            cmd.Parameters.AddWithValue("@pass", user.Password);
            cmd.Parameters.AddWithValue("@city", user.City);
            cmd.Parameters.AddWithValue("@street", user.Street);
            cmd.Parameters.AddWithValue("@zip", user.Zip);
            cmd.Parameters.AddWithValue("@flat_number", user.FlatNumber);
            if (( user.Id >0))
                cmd.Parameters.AddWithValue("@id", user.Id);
            cmd.ExecuteNonQuery();

            return user;
        }

        //przy usuwaniu ustawiamy flagę active na false
        public void deleteUser(User user)
        {
            String query = "UPDATE users SET active=0 WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", user.Id);
            cmd.ExecuteNonQuery();
             
        }

        //funkcja sprawdzająca poprawność logowania
        public bool checkLogin(String login, String pass)
        {
            String query = "Select pass FROM users WHERE login=@login";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@login", login);
            String password = (String)cmd.ExecuteScalar();
            if (pass == null)
                return false;
            String encodepass = Global.EncodePassword(pass);
            if (encodepass.Equals(password))
                return true;
            return false;
        }

        //sprawdza czy dany login już istnieje
        public bool checkIfLoginExists(String login)
        {
            String query = "Select login FROM users WHERE login=@login";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@login", login);
            String logintemp = (String)cmd.ExecuteScalar();

            if (logintemp == null)
                return false;

            return true;
        }

        public User getUserByLogin(String login)
        {
            String query = "SELECT * from users WHERE login=@login";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@login", login);
            SqlDataReader rdr = cmd.ExecuteReader();
            if (!rdr.HasRows)
                return null;
            rdr.Read();
            User user = new User();
            user.Id = rdr.GetInt32(rdr.GetOrdinal("id"));
            user.Name = rdr.GetString(rdr.GetOrdinal("name"));
            user.SurName = rdr.GetString(rdr.GetOrdinal("surname"));
            user.Active = rdr.GetBoolean(rdr.GetOrdinal("active"));
            user.BirthDate = rdr.GetDateTime(rdr.GetOrdinal("birth_date"));
            user.City = rdr.GetString(rdr.GetOrdinal("city"));
            user.Country = rdr.GetString(rdr.GetOrdinal("country"));
            user.Login = rdr.GetString(rdr.GetOrdinal("login"));
            user.Password = rdr.GetString(rdr.GetOrdinal("pass"));
            user.Role = rdr.GetInt32(rdr.GetOrdinal("role"));
            int salonId = rdr.GetInt32(rdr.GetOrdinal("salon"));
            user.Salon = ServiceFactory.getAutoSalonService().getAutoSalonById(salonId);

            user.Street = rdr.GetString(rdr.GetOrdinal("street"));
            user.Zip = rdr.GetString(rdr.GetOrdinal("zip"));
            user.FlatNumber = rdr.GetString(rdr.GetOrdinal("flat_number"));
            rdr.Close();
            return user;
        }
        public User getUserById(int id)
        {
            String query = "SELECT * from users WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", id);
            SqlDataReader rdr = cmd.ExecuteReader();
            if (!rdr.HasRows)
                return null;
            rdr.Read();
            User user = new User();
            user.Id = rdr.GetInt32(rdr.GetOrdinal("id"));
            user.Name = rdr.GetString(rdr.GetOrdinal("name"));
            user.SurName = rdr.GetString(rdr.GetOrdinal("surname"));
            user.Active = rdr.GetBoolean(rdr.GetOrdinal("active"));
            user.BirthDate = rdr.GetDateTime(rdr.GetOrdinal("birth_date"));
            user.City = rdr.GetString(rdr.GetOrdinal("city"));
            user.Country = rdr.GetString(rdr.GetOrdinal("country"));
            user.Login = rdr.GetString(rdr.GetOrdinal("login"));
            user.Password = rdr.GetString(rdr.GetOrdinal("pass"));
            user.Role = rdr.GetInt32(rdr.GetOrdinal("role"));
            int salonId = rdr.GetInt32(rdr.GetOrdinal("salon"));
            user.Salon = ServiceFactory.getAutoSalonService().getAutoSalonById(salonId);

            user.Street = rdr.GetString(rdr.GetOrdinal("street"));
            user.Zip = rdr.GetString(rdr.GetOrdinal("zip"));
            user.FlatNumber = rdr.GetString(rdr.GetOrdinal("flat_number"));
            rdr.Close();
            return user;
        }

        /// <summary>
        /// Zwraca listę obiektów typu User na podstawie listy kontrolek filtrujących
        /// </summary>
        /// <param name="filterList"></param>
        /// <returns></returns>
        public IList<User> getUsers(IList<IFilter> filterList)
        {
            IList<User> users = new List<User>();
            String query = "SELECT * FROM users WHERE active=1 ";
            query += Global.getConditionString(filterList);

            SqlCommand command = SqlService.getInstance().getCommand(query);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                { 
                    User user = new User();
                    user.Id = reader.GetInt32(reader.GetOrdinal("id"));
                    user.Name = reader.GetString(reader.GetOrdinal("name"));
                    user.SurName = reader.GetString(reader.GetOrdinal("surname"));
                    user.Login = reader.GetString(reader.GetOrdinal("login"));
                    user.Country = reader.GetString(reader.GetOrdinal("country"));
                    user.City = reader.GetString(reader.GetOrdinal("city"));
                    user.Street = reader.GetString(reader.GetOrdinal("street"));
                    user.Zip = reader.GetString(reader.GetOrdinal("zip"));
                    user.FlatNumber = reader.GetString(reader.GetOrdinal("flat_number"));
                    int salonId = reader.GetInt32(reader.GetOrdinal("salon"));
                    user.Salon = ServiceFactory.getAutoSalonService().getAutoSalonById(salonId);
                    user.BirthDate = reader.GetDateTime(reader.GetOrdinal("birth_date"));
                    user.Role = reader.GetInt32(reader.GetOrdinal("role"));

                    users.Add(user);
                }
            }

            reader.Close();

            return users;
        }

        public IList<DictionaryItem> getUsersToCombo()
        {
            IList<DictionaryItem> users = new List<DictionaryItem>();
            users.Add(new DictionaryItem(0,""));
            String query = "SELECT id,name,surname FROM users WHERE active=1 ";
            SqlCommand command = SqlService.getInstance().getCommand(query);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    DictionaryItem di = new DictionaryItem();
                    di.Id = reader.GetInt32(0);
                    di.DictValue = reader.GetString(1) + " " + reader.GetString(2);

                    users.Add(di);
                }
            }
            reader.Close();

            return users;
        }
    }
}
